/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */


#ifndef SOUNDSOURCE_H_
#define SOUNDSOURCE_H_

#include "Ogg/OggFile.h"

#include <QSharedPointer>
#include <QVector3D>

#include "AL/al.h"

class SoundSource {
	friend class SoundManager;
	friend class VideoPlayer;
	typedef QSharedPointer<ALuint> OpenAlBuffers;
	enum SourceType {
		ST_STATIC = 0x1028, ST_STREAMING = 0x1029, ST_UNDETERMINED = 0x1030
	};
public:
	virtual ~SoundSource();
	bool startPlayback();
	bool stopPlayback();
	bool isPlaying();

	float getPitch();
	float getGain();
	float getMaxDistance();
	float getRollOffFactor();
	float getMaxGain();
	float getConeOuterGain();
	float getConeInnerAngle();
	float getConeOuterAngle();
	QVector3D getPosition();
	QVector3D getVelocity();
	QVector3D getDirection();
	bool isSourceRelative();
	SourceType getSourceType();
	bool isLoop();

	bool setPitch(float rPitch);
	bool setGain(float rGain);
	bool setMaxDistance(float rDistance);
	bool setRollOffFactor(float rRollOffFactor);
	bool setMaxGain(float rMaxGain);
	bool setConeOuterGain(float rOuterGain);
	bool setConeInnerAngle(float rAngle);
	bool setConeOuterAngle(float rAngle);
	bool setPosition(float rX, float rY, float rZ);
	bool setPosition(const QVector3D& rVector);
	bool setVelocity(float rX, float rY, float rZ);
	bool setVelocity(const QVector3D& rVector);
	bool setDirection(float rX, float rY, float rZ);
	bool setDirection(const QVector3D& rVector);
	bool setSourceRelative(bool rOnOff);
	bool setSourceType(SourceType rType);
	bool setLoop(bool rOnOff);

protected:
	SoundSource(ALuint rNumBuffers, OggFilePtr rFile);

private:
	void release();
	void empty();
	bool update();
	bool stream(ALuint rBuffer);
	void checkFormat();
	QString errorString(int rCode);

	inline bool check() {
		return (alGetError() == AL_NO_ERROR);
	}

protected:
	ALsizei mBufferSize;
	ALenum mFormat;
	ALuint mNumBuffers;
	OpenAlBuffers mBuffers;
	ALuint mSource;
	OggFilePtr mOggFile;
};

typedef QSharedPointer<SoundSource> SoundSourcePtr;

#endif /* SOUNDSOURCE_H_ */
